package com.daybyday.day1.day4;

import org.omg.PortableInterceptor.INACTIVE;

import javax.sound.midi.Soundbank;
import java.util.*;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2020/11/9

*/
public class Solution {

    public static void main(String[] args) {
        Solution s = new Solution();
        int[] nums = {1,2,1};
        s.totalFruit(nums);
    }

    public int totalFruit(int[] tree) {
        int max = 0;
        int count = 0;
        int pos = 0;
        Set<Integer> list = new HashSet<>();
        for (int i = 0; i < tree.length; i++) {
            if (list.size() < 2 ||(list.size() == 2 && list.contains(tree[i]))){
                list.add(tree[i]);
                count ++ ;
            } else{
                max = max > count ? max : count;
                count = 0;
                list.clear();
                list.add(tree[pos + 1]);
                i = pos--;


            }
            if (list.size() == 1){
                pos = i;
            }

        }
        return  max > count ? max : count;
    }

    public int minFlipsMonoIncr(String S) {
        return 0;
    }
    public static  void add(Integer a,int b){

    }
    public static  void add(int a, Integer b){

    }

    public int numSubseq(int[] nums, int target) {
        Arrays.sort(nums);
        int res = 0;
        int j = 0;
        int i = nums.length - 1;
        if (nums[0] > target){
            return 0;
        }
        while (j <= i){
            if (nums[j] + nums[i] > target){
                i--;
            }else{
                res += Math.pow(2,i-j) ;
                j++;
            }
        }
        return res;
    }

    public int findMaximumXOR(int[] nums) {
        int res = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length-1; i++) {
            if ((nums[i] ^ nums[i + 1]) > res){
                res = nums[i] ^ nums[i + 1];
            }
        }
        return res;
    }

    public int[] searchRange(int[] nums,int target){
        int start = 0;
        int end = nums.length - 1;
        int[] res = new int[]{-1,-1};
       /* if (nums[0] > target || nums[nums.length - 1] < target){
            return res;
        }*/
        return getTarget(nums,start,end,target,res);
    }

    public int[]  getTarget(int[] nums,int start, int end,int target,int[] res){
        while (start<=end){
            int mid = (start + end) / 2;
            if (nums[mid] > target){
                end = mid - 1;
                getTarget(nums,start,end,target,res);
            }else if (nums[mid] < target){
                start = mid+1;
                getTarget(nums,start,end,target,res);
            }else if(nums[mid] == target){
                int i = mid;int j = mid;
                while (i >= 0){
                    if (nums[i] != target){
                        break;
                    }
                    i--;
                }
                while (j < nums.length){
                    if (nums[j] != target){
                        break;
                    }
                    j++;
                }
                res[0] = i+1;
                res[1] = j-1;

            }
        }
            return res;
    }



    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;int i = 0;
        while (j<popped.length){

            if ((stack.size()!=0) && (stack.peek() == popped[j])){
                stack.pop();
                j++;
            }else{
                if (i >= pushed.length){
                    return false;
                }
                if (pushed[i] != popped[j]){
                    stack.push(pushed[i]);
                    i++;
                    continue;
                }
                j++;
                i++;
                continue;
            }
        }
        return true;
    }

    public List<Integer> luckyNumbers (int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        HashMap<Integer,Integer> map = new HashMap<>();
        HashMap<Integer,Integer> mapMin = new HashMap<>();
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                map.put(j,matrix[i][j]);
            }
            mapMin.put(i,getMin(map));
        }
        HashMap<Integer,Integer> mapMax = new HashMap<>();
        for (Integer value : map.values()) {

        }
        return new ArrayList<>();
    }

    public int getMax(HashMap<Integer,Integer> map){
        int max =  Integer.MIN_VALUE ;
        for (Integer integer : map.keySet()) {
            if(map.get(integer) >=max) max = integer;
        }
        return max;
    }

    public int getMin(HashMap<Integer,Integer> map){
        int min = Integer.MAX_VALUE;
        for (Integer integer : map.keySet()) {
            if(map.get(integer) < min) min = integer;
        }
        return min;
    }




    public void frequencySort(int[] nums) {
        Map<String, Integer> map = new HashMap<>();


        for (int num : nums) {
            if (map.keySet().contains(num + "")) {
                map.put(num + "", map.get(num + "") + 1);
            } else {
                map.put(num + "", 1);
            }
        }
        for (String s : map.keySet()) {
            System.out.println(s + "  " +  map.get(s));
        }

        List<Map.Entry<String, Integer>> infoIds =
                new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

        Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue() - o2.getValue();
            }
        });
        System.out.println("-------------------------------------------");

        for (String s : map.keySet()) {
            System.out.println(s + "  " +  map.get(s));
        }
        char c = 'c';
        String s = "hello";


    }

}
